package com.lee.common.util;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Array;
import java.util.function.Function;
import java.util.function.IntFunction;

/**
 * 用于将简单数组转字符串或字符串转简单数组
 *
 * @author Lee HN
 * @date 2021/3/28 10:37
 */
public final class ArrayStringUtil {

    private static final char separator = ' ';

    @SafeVarargs
    public static <E> String toString(E... array) {
        return array == null || array.length == 0 ? "" : StringUtils.join(array, separator);
    }

    public static <E> E[] toArray(String string, Function<String, E> function, IntFunction<E[]> generator) {
        String[] strings = StringUtils.split(string, separator);
        E[] array;
        if (strings != null) {
            int length = strings.length;
            array = generator.apply(length);
            for (int i = 0; i < length; i++)
                array[i] = function.apply(strings[i]);
        } else array = generator.apply(0);
        return array;
    }

    @SuppressWarnings("unchecked")
    public static <E> E[] toArray(String string, Function<String, E> function) {
        String[] strings = StringUtils.split(string, separator);
        Class<?> type = function.apply("0").getClass();
        E[] array;
        if (strings != null) {
            int length = strings.length;
            array = (E[]) Array.newInstance(type, length);
            for (int i = 0; i < length; i++)
                array[i] = function.apply(strings[i]);
        } else array = (E[]) Array.newInstance(type, 0);
        return array;
    }

    public static String toString(int... array) {
        return array == null || array.length == 0 ? "" : StringUtils.join(array, separator);
    }

    public static int[] toIntArray(String string) {
        String[] strings = StringUtils.split(string, separator);
        int[] array;
        if (strings != null) {
            int length = strings.length;
            array = new int[length];
            for (int i = 0; i < length; i++)
                array[i] = Integer.parseInt(strings[i]);
        } else array = ArrayUtils.EMPTY_INT_ARRAY;
        return array;
    }

    public static String toString(long... array) {
        return array == null || array.length == 0 ? "" : StringUtils.join(array, separator);
    }

    public static long[] toLongArray(String string) {
        String[] strings = StringUtils.split(string, separator);
        long[] array;
        if (strings != null) {
            int length = strings.length;
            array = new long[length];
            for (int i = 0; i < length; i++)
                array[i] = Long.parseLong(strings[i]);
        } else array = ArrayUtils.EMPTY_LONG_ARRAY;
        return array;
    }

    public static String toString(double... array) {
        return array == null || array.length == 0 ? "" : StringUtils.join(array, separator);
    }

    public static double[] toDoubleArray(String string) {
        String[] strings = StringUtils.split(string, separator);
        double[] array;
        if (strings != null) {
            int length = strings.length;
            array = new double[length];
            for (int i = 0; i < length; i++)
                array[i] = Double.parseDouble(strings[i]);
        } else array = ArrayUtils.EMPTY_DOUBLE_ARRAY;
        return array;
    }

    public static String join(final Object[] array, final char separator, final int startIndex, final int endIndex) {
        if (array == null) {
            return null;
        }
        final int noOfItems = endIndex - startIndex;
        if (noOfItems <= 0) {
            return "";
        }
        final StringBuilder buf = new StringBuilder(noOfItems * 16);
        if (array[startIndex] != null) {
            buf.append(array[startIndex]);
        }
        for (int i = startIndex + 1; i < endIndex; i++) {
            buf.append(separator);
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }

}
